package com.hsyt.pms.api.vo.org;

import com.hsyt.pms.api.dto.org.*;
import com.hsyt.pms.api.entity.org.*;
import com.hsyt.pms.api.enums.OrgType;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: TODO
 * @author wangze@hsyuntai.com
 * @date 2019年2月12日 下午2:45:51
 */
public class OrgHelp {

    public static final String INIT_SYS_ORG_CODE = "1001";

    public final static Org createOrg(Integer orgType) {
        OrgType ot = OrgType.getOrgType(orgType);
        if (ot == null) return new Org();
        switch (ot) {
            case FIRE:
                return new OrgFire();
            case SOCIAL:
                return new OrgSocial();
            case INDUSTRY:
                return new OrgIndustry();
            case BUSINESS:
                return new OrgBusiness();
            case TENEMENT:
                return new OrgTenement();
            case ACCEPT_CENTER:
                return new OrgAcceptCenter();
            case MAINTENANCE:
                return new OrgMaintenance();
            case MICRO_FIRE:
                return new OrgMicroFire();
            default:
                return new Org();
        }

    }
    public final static OrgDto createOrgDto(Integer orgType) {
        OrgType ot = OrgType.getOrgType(orgType);
        if (ot == null) return new OrgDto();
        switch (ot) {
            case FIRE:
                return new OrgFireDto();
            case SOCIAL:
                return new OrgSocialDto();
            case INDUSTRY:
                return new OrgIndustryDto();
            case BUSINESS:
                return new OrgBusinessDto();
            case TENEMENT:
                return new OrgTenementDto();
            case ACCEPT_CENTER:
                return new OrgAcceptCenterDto();
            case MAINTENANCE:
                return new OrgMaintenanceDto();
            case MICRO_FIRE:
                return new OrgMicroFireDto();
            default:
                return new OrgDto();
        }

    }

    public final static Class getOrgDtoClass(Integer orgType) {
        OrgType ot = OrgType.getOrgType(orgType);
        if (ot == null) return OrgDto.class;
        switch (ot) {
            case FIRE:
                return OrgFireDto.class;
            case SOCIAL:
                return OrgSocialDto.class;
            case INDUSTRY:
                return OrgIndustryDto.class;
            case BUSINESS:
                return OrgBusinessDto.class;
            case TENEMENT:
                return OrgTenementDto.class;
            case ACCEPT_CENTER:
                return OrgAcceptCenterDto.class;
            case MAINTENANCE:
                return OrgMaintenanceDto.class;
            case MICRO_FIRE:
                return OrgMicroFireDto.class;
            default:
                return OrgDto.class;
        }

    }

    //    public final static OrgDto createOrgDto(String orgType) {
//
//    }

    public final static String nextOrgCode(String maxSysOrgCode) {
        StringBuffer sb = new StringBuffer();
        if (maxSysOrgCode != null) {
            int pos = maxSysOrgCode.lastIndexOf('_');
            if (pos > 0) {
                sb.append(Integer.parseInt(maxSysOrgCode.substring(pos + 1)) + 1);
            } else {
                sb.append(Integer.parseInt(maxSysOrgCode) + 1);
            }
        } else {
            sb.append(INIT_SYS_ORG_CODE);
        }
        return sb.toString();
    }

    public final static String nextSysOrgCode(String maxSysOrgCode) {
        StringBuffer sb = new StringBuffer();
        if (maxSysOrgCode != null) {
            int pos = maxSysOrgCode.lastIndexOf('_');
            if (pos > 0) {
                sb.append(maxSysOrgCode.substring(0, pos+1));
                sb.append(Integer.parseInt(maxSysOrgCode.substring(pos + 1)) + 1);
            } else {
                sb.append(Integer.parseInt(maxSysOrgCode) + 1);
            }
        } else {
            sb.append(INIT_SYS_ORG_CODE);
        }
        return sb.toString();
    }
    public final static String makeParentSysOrgCode(String sysOrgCode) {
        StringBuffer sb = new StringBuffer();
        if (sysOrgCode != null) {
            int pos = sysOrgCode.lastIndexOf('_');
            if (pos > 0) {
                sb.append(sysOrgCode.substring(0, pos));
            }
        }
        return sb.toString();
    }
    public final static int makeOrgLevel(String sysOrgCode) {
        int level = StringUtils.countMatches(sysOrgCode, "_");
        return level + 1;
    }

    public static void main(String[] args) {
        Org org = new Org();
        org.setParentOrgCode("1001");
        org.setOrgCode("1003");
        org.setSysOrgCode("1001_1003");
        org.setOrgLevel(2);
        String maxOrgSysCode = null;
//        String maxOrgSysCode = "1002";
//        String maxOrgSysCode = "1001_1013";
        String nextOrgSysCode = nextSysOrgCode(maxOrgSysCode);
        System.out.println("nextOrgCode->" + nextOrgCode(maxOrgSysCode));

        System.out.println("nextOrgSysCode->" + nextOrgSysCode);
        System.out.println("makeParentSysOrgCode->" + makeParentSysOrgCode("8001_1004_1009_1012"));
        System.out.println("makeOrgLevel->" + makeOrgLevel("8001_1004_1009"));

    }
}
